package com.zfast.config.junit.wx;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

/**
 * @author zgn
 * @Description: simJunit
 * @date 2022/1/13 0013
 */
public class wxJunit {
    @Test
    public void egMax() {//e.g.
        //region 1.模拟数据
        System.out.println("--------------------Start--------------------");
        //endregion

        //region 2.数据处理
        System.out.println("--------------------Deal--------------------");
        //endregion

        //region 3.数据展示
        System.out.println("--------------------Done--------------------");
        //endregion
    }

    @Test
    @SneakyThrows
    public void 修改对应小程序名称_egMin() {//e.g.
        String access_token="";
        String component_verify_ticket=getComponentVerifyTicket(access_token);
        String component_access_token=getComponentAccessToken(component_verify_ticket);
        String accessToken = getAccessToken(component_access_token);

        String newName = "小说阅读器";
        JSONObject requestBody = new JSONObject();
        requestBody.put("name", newName);

        boolean result = modifyMiniProgramName(accessToken, requestBody.toString());
        if (result) {
            System.out.println("小程序名称修改成功");
        } else {
            System.out.println("小程序名称修改失败");
        }
    }

    /**
     * 验证票据
     * @author  zgn
     * @date    2024/3/20
     * @param	access_token
     * @return	java.lang.String
     */
    private String getComponentVerifyTicket(String access_token) {
        HashMap<String, Object> paramMap = new HashMap<>();
        //String component_access_token1 = HttpUtil.post(GET_COMPONENT_VERIFY_TICKET_URL.replace("ACCESS_TOKEN", access_token),paramMap);
        return null;
    }

    /**
     * 令牌
     * @author  zgn
     * @date    2024/3/20
     * @param	access_token
     * @return	java.lang.String
     */
    private String getComponentAccessToken(String access_token) {
        HashMap<String, Object> paramMap = new HashMap<>();
        String component_access_token1 = HttpUtil.post(GET_COMPONENT_ACCESS_TOKEN_URL.replace("ACCESS_TOKEN", access_token),paramMap);
        return null;
    }

    private static final String APPID = "wx95985f36ad38199d";
    private static final String APPSECRET = "f1750b66926768c87b5c50121096eb5a";
    //private static final String GET_COMPONENT_VERIFY_TICKET_URL = "https://api.weixin.qq.com/wxa/setbetaweappnickname?access_token=ACCESS_TOKEN";
    private static final String GET_COMPONENT_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/wxa/setbetaweappnickname?access_token=ACCESS_TOKEN";
    private static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=COMPONENT_ACCESS_TOKEN";
    private static final String MODIFY_NAME_URL = "https://api.weixin.qq.com/wxa/setbetaweappnickname?access_token=ACCESS_TOKEN";


    /**
     * 获取/刷新接口调用令牌
     * @author  zgn
     * @date    2024/3/20
     * @param	component_access_token
     * @return	java.lang.String
     */
    private static String getAccessToken(String component_access_token) throws Exception {
        /*URL url = new URL(GET_TOKEN_URL.replace("COMPONENT_ACCESS_TOKEN",component_access_token));
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Accept", "application/json");

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        String output;
        StringBuilder response = new StringBuilder();
        while ((output = br.readLine()) != null) {
            response.append(output);
        }
        conn.disconnect();

        JSONObject jsonObj = new JSONObject(response.toString());
        return jsonObj.get("access_token").toString();*/
        HashMap<String, Object> paramMap = new HashMap<>();
        String component_access_token1 = HttpUtil.post(GET_TOKEN_URL.replace("COMPONENT_ACCESS_TOKEN", component_access_token),paramMap);
        return null;
    }

    /**
     * 修改试用小程序名称
     * @author  zgn
     * @date    2024/3/20
     * @param	accessToken
     * @param	requestBody
     * @return	java.lang.String
     */
    private static boolean modifyMiniProgramName(String accessToken, String requestBody) throws Exception {
        URL url = new URL(MODIFY_NAME_URL.replace("ACCESS_TOKEN",accessToken) );
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setDoOutput(true);

        try (java.io.OutputStream os = conn.getOutputStream()) {
            byte[] input = requestBody.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        String output;
        StringBuilder response = new StringBuilder();
        while ((output = br.readLine()) != null) {
            response.append(output);
        }
        conn.disconnect();

        JSONObject jsonObj = new JSONObject(response.toString());
        return jsonObj.getInt("errcode") == 0; // errcode 为 0 表示成功
    }

    @Test
    public void sim_egMin() {//e.g.
        String component_access_token="";
        HashMap<String, Object> paramMap = new HashMap<>();
        String component_access_token1 = HttpUtil.post(GET_TOKEN_URL.replace("COMPONENT_ACCESS_TOKEN", component_access_token),paramMap);
        System.out.println(component_access_token1);
    }

    @Test
    public void base64加密解密_egMin() {//e.g.
        Base64 base64=new Base64();
        System.out.println();
    }

    @Test
    public void egMin() {//e.g.
        System.out.println();
    }
}
